<?php session_start();
	$unit = $_REQUEST['unit']; ?>
	var details;
	var unit;
	
	function setUnit(unit_name){
		unit=unit_name;
	}
	function viewAccountForm(){

		$("#accountForm2").html("<img src='gifs/indicator.gif' />");
		
		$("#accountForm2").dialog({	
			modal: true,
			resizable: false,
			show: "blind",
			hide: "explode",
			buttons: {
				Close : function() {
					$(this).dialog('close');
				}
			}
		});
		
		$("#accountForm2").load('controllers/ViewAccountController.php?mode=1&unit_name='+unit,function(data) {
		$("#editaccountform5").validate({
			  rules: {
				name: {
					required:true,
					minlength:5,
					maxlength:60
				},
				home_address: {
					required:true,
					maxlength:99,
					minlength:5
				},
				studnum: {
					required:true,
					maxlength:10
				},
				cellnum: {
					required:true,
					maxlength:11,
					digits:true
				},
				course: {
					required:true,
					maxlength:50
				},
				college: {
					required:true,
					maxlength:60
				},
				classification: {
					maxlength:15
				},
				parents_name: {
					maxlength:100
				},
				contact_nums: {
					maxlength:30
				},
				other_occupants: {
					maxlength:300
				},
			  }
			});
		$("input:submit", "#accountForm2").button();
		  details=data; 	

		});
	}
	
	function viewFinancialForm(){
		$("#financeForm2").html("<img src='gifs/indicator.gif' />");
		$("#financeForm2").dialog({	
			modal: true,
			resizable: false,
			show: "blind",
			hide: "explode",
			buttons: {
				Close : function() {
					$(this).dialog('close');
				}
			}
		});
		$("#financeForm2").load('controllers/ViewFinancialInformationController.php?mode=1&unit_name='+unit,function(data) {
		$("#editfinancialinfoform5").validate({
			rules: {
				deposit: {
					required:true,
					number:true,
					max:99999,
					min:0								
				},
				debts: {
					required:true,
					number:true,
					max:99999,								
					min:0
				}
			}
		});
		$("input:submit", "#financeForm2").button();
		  details=data;
		});
	}
	
						function viewUnitAccount(){
						$.get("ajax/templates/viewaccount.php?unit="+"<?php echo $_SESSION['username']?>", function(data){
						//alert($("#account").html());
						$("#units2").append(data);
					
						viewAccount();
						viewFinancialInfo();
						});
					
					}
	
	function viewAccount(){
		$("input:submit","div").button();
		<?php if($_SESSION['username']!=admin){?>
		setUnit("<?php echo $_SESSION['username']?>");
		<?php }?>
		
		$("#accountForm").html("<img src='gifs/indicator.gif' />");
		$.get('controllers/ViewAccountController.php?mode=2&unit_name='+unit, function(data){
			details=data;
			displayAccountDetails();
			$("#paymentstable").tablesorter(			
			{
				widgets: ['zebra'],
				headers: { 
            // assign the secound column (we start counting zero) 
					3: { 
						// disable it by setting the property sorter to false 
						sorter: "digit"
					} 
				} 
			}
			
			); 
			$("#invoices").tablesorter(
			
			{
				widgets: ['zebra'],
				headers: { 
            // assign the secound column (we start counting zero) 
					2: { 
						// disable it by setting the property sorter to false 
						sorter: false 
					},
					3: { 
						// disable it by setting the property sorter to false 
						sorter: "digit"
					},
					4: { 
						// disable it by setting the property sorter to false 
						sorter: "digit"
					} 
			} 
			}
			
			
			
			); 
			$("input:button", "#accountForm").button();
			
		});
	}
	
	function displayAccountDetails(){
		$("#accountForm").html("");
		$("#accountForm").append(details);
	}
	function viewFinancialInfo(){
		$("#financeForm").html("<img src='gifs/indicator.gif' />");
		$("#financeForm").load('controllers/ViewFinancialInformationController.php?mode=2&unit_name='+unit,function(data) {
			$("input:button", "#financeForm").button();
		});
	}
	
	function updateAccountInfo(){
		name1=$("#name").val();
		home=$("#home_address").val();
		studnum=$("#studnum").val();
		cellnum=$("#cellnum").val();
		course=$("#course").val();
		college=$("#college").val();
		classification=$("#classification").val();
		parents=$("#parents_name").val();
		contact=$("#contact_nums").val();
		occupants=$("#other_occupants").val();
		$("#accountForm").html("<img src='gifs/indicator.gif' /><br />Updating...");
		
		$.post('controllers/UpdateAccountController.php',{
		name:name1,
		home:home,
		studnum:studnum,
		cellnum:cellnum,
		course:course,
		college:college,
		classification:classification,
		parents:parents,
		contact:contact,
		occupants:occupants,
		unitname:unit,
		}, function(data) {
			details=data;
			
			displayUpdateAccountDetails();
		  viewAccount();
		  
		});
	}
	
	function displayUpdateAccountDetails(){
		$("#accountForm2").html(details);
	}

	
	//LOGOUT
	function logout(){
		
			$("#logout").html("Are you sure you want to log out?");
			$("#logout").dialog({
				resizable: false,
				modal: true,
				position: 'top',
				buttons: {
					Cancel: function() {
						$(this).dialog('close');
					},
					Ok: function() {
						$("#logout").html("You will be redirected within 2 seconds.");
						//exit();
						
							$("#logout").load('controllers/LogOutController.php',function(data) {
								//exit();
							});
						

					}
				}
			});
			//$("#logout").html("<img src='gifs/indicator.gif' /> Logging out...");
		}
	
	
	function viewRequestDialog(){
			$("#addrequestdialog").load('ajax/forms/requestform.php',function(data) {
			$("#addrequestform5").validate({
				rules: {
					amount: {
						required:true,
						number:true,
						max:99999,
						maxlength:7,
						min:0								
					},
					mode: {
						required:true,
						maxlength:15
					},
					date_received: {
						date:true,
						required:true
					},
					receipt_no: {
						maxlength:10
					},
					message: {
						required:true,
						maxlength:300
					}
					
				}
			});
			$("input:submit", "#addrequestdialog").button();
			$("#addrequestdialog").dialog({	
			modal: true,
			resizable: false,
			show: "blind",
			hide: "explode",
			buttons: {
				Close : function() {
					$(this).dialog('close');
				}
			}
		});
		});
	

	}
	
	function sendRequest(){
		setUnit("<?php echo $_SESSION['username'];?>");
		amount=$("#amount").val();
		mode=$("#mode").val();
		date_received=$("#date_received").val();
		receipt_no=$("#receipt_no").val();
		message=$("#message").val();
		$("#requestForm").html("<img src='gifs/indicator.gif' />");
		
		$.post('controllers/RequestUpdatePaymentController.php',{ 
		amount: amount, 
		mode: mode, 
		date_received: date_received, 
		receipt_no: receipt_no, 
		message: message, 
		unit:unit,
		}, function(data) {
		details=data;
		displayAddRequestDetails();
			$("#requestsList").load('controllers/ViewRequestController.php?mode=1',function(data) {
		  details=data;
		  displayRequestsList();
			});
		
		 
		});
	}
	
	function displayAddRequestDetails(){
		 $("#addrequestdialog").html(details);
	}

	
	//FOR ADMINISTRATOR ONLY
	<?php if(($_SESSION['username'])=="admin"){?>
	

	
	//UPDATE FINANCIAL INFORMATION
	function updateFinancialInfo(){
		deposit=$("#deposit").val();
		debts=$("#debts").val();
		$("#financeForm").html("<img src='gifs/indicator.gif' /><br />Updating...");
		$.post('controllers/UpdateFinancialInformationController.php',{
		deposit:deposit,
		debts:debts,
		unit_name:unit
		}, function(data) {
			details=data;
			displayUpdateFinancialInfoDetails();
		  viewFinancialInfo();
		});
	}
	
	function displayUpdateFinancialInfoDetails(){
		$("#financeForm2").html(details);
	}
	
	//RECOVER PASSWORD
		function recoverPassword(){
			$("#recover").dialog({
				resizable: false,
				modal: true,
				position: 'top',
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				}
			});
			$("#recover").html("<img src='gifs/indicator.gif' /> Sending password...");
			$.get('controllers/RecoverPasswordController.php',function(data) {
				details=data;
				displayRecoverPasswordDetails();
			});
		}

		function displayRecoverPasswordDetails(){
			$("#recover").html(details);
		}
		var test;

		

	
	
	//STOP CONTRACT
	function requestStopContract(){
		$("#stopcontract").html("<img src='gifs/indicator.gif' />");
		$.get('controllers/StopContractController.php?unit_name='+unit,function(data) {
			details=data;
			viewStopContractDetails();
			viewAccount();
			viewFinancialInfo();
		});
	}
	
	function viewStopContractDetails(){
		$("#stopcontract").html(details);
	}

	function stopContract2(){
		$("#stopContractDialog").html("Are you sure you want to stop the contract for Unit "+unit+"?");
		$(function() {
			$("#stopContractDialog").dialog({
				resizable: false,
				modal: true,
				position: 'top',
				buttons: {
					Cancel: function() {
						$(this).dialog('close');
					},
					'Stop Contract': function() {
						requestStopContract();
						$("#units2").load("ajax/templates/viewaccount.php?unit="+unit,function(){
						//	$("#units").fadeIn('slow');
							setUnit(unit);
							viewAccount();
							viewFinancialInfo();
						});
						$(this).dialog('close');
					}
				}
			});
		});
	}
	
	
	
	//START CONTRACT
	var details;
	function sendContractData(){
		password1=$("#password").val();
		name1=$("#name").val();
		home=$("#home_address").val();
		studnum=$("#studnum").val();
		cellnum=$("#cell_num").val();
		course=$("#course").val();
		college=$("#college").val();
		classification=$("#classification").val();
		parents=$("#parents_name").val();
		contact=$("#contact_nums").val();
		occupants=$("#other_occupants").val();
		deposit=$("#deposit").val();
		rent=$("#monthly_rent").val();
		start=$("#start_date").val();
		debts=$("#debts").val();
		$.post('controllers/StartContractController.php',{ 
		name: name1, 
		password: password1,
		home: home,
		studnum:studnum,
		cellnum:cellnum,
		course:course,
		college:college,
		classification: classification,
		parents:parents,
		contact:contact,
		occupants:occupants,
		deposit:deposit,
		rent:rent,
		start:start,
		debts:debts,
		unit_name:unit
		}, function(data) {
		  details=data;
			displayContractDetails();
		});		
	}

	function displayContractDetails(){
		$('#contractForm').html(details);
	}

	function viewStartContractForm(){
	
		$("#contractForm").html("<img src='gifs/indicator.gif' /><br />Updating...");
		$("#contractForm").load('ajax/forms/startcontractform.php',function(){
						$("#start_date").datepicker({ dateFormat: 'yy-mm-dd' });
						$("input:submit", "#contractForm").button();
						$("#startcontractform5").validate({
							rules: {
							password: {
								required:true,
								minlength:5,
								maxlength:20
							},
							name: {
								required:true,
								minlength:5,
								maxlength:60
							},
							home_address: {
								required:true,
								minlength:5,
								maxlength:20
							},
							studnum: {
								required:true,
								maxlength:10
							},
							cell_num: {
								required:true,
								maxlength:11,
								digits:true
							},
							course: {
								required:true,
								maxlength:50
							},
							college: {
								required:true,
								maxlength:60
							},
							classification: {
								maxlength:15
							},
							parents_name: {
								maxlength:100
							},
							contact_nums: {
								maxlength:30
							},
							other_occupants: {
								maxlength:300
							},
							deposit: {
								required:true,
								number:true,
								max:99999,
								min:0								
							},
							monthly_rent: {
								required:true,
								number:true,
								max:99999,
								min:0								
							},
							start_date: {
								required:true,
								date:true								
							},
							debts: {
								required:true,
								number:true,
								max:99999,								
								min:0
							}
						}
					});
		});
		
		$("#contractForm").dialog({
			modal: true,
			position:top,
			resizable: false,
			show: "blind",
			hide: "explode",
			buttons: {
				Close: function() {
					//$(this).dialog('close',function(){
					$(this).dialog('close');
						$("#units2").load("ajax/templates/viewaccount.php?unit="+unit,function(){
						setUnit(unit);
						viewAccount();
						viewFinancialInfo();
					  
					//  });
					
					});
					
					
				}
			}
			});
		
	}
	
	
	
	
	//ADD PAYMENT
	function sendPayment(){
		amount=$("#amount").val();
		unit=$("#unit").val();
		mode=$("#mode").val();
		date_received=$("#date_received").val();
		receipt_no=$("#receipt_no").val();
		$("#addpaymentform").html("<img src='gifs/indicator.gif' />");
		$.post('controllers/AddPaymentController.php',{
		unit: unit,
		amount: amount, 
		mode: mode, 
		date_received: date_received, 
		receipt_no: receipt_no, 
		}, function(data) {
		  details=data;
			displayPaymentDetails();
		});
	}
	function displayPaymentDetails(){
		//alert(details);
		$("#addpaymentform").html(details);
		
	}

	function addpayment(){
		$("#addpaymentform").load('ajax/forms/addpaymentform.php',function(){
		$("input:submit", "#addpaymentform").button();
					var units = [
			"A1","A2","A3","A4","A5","A6","A7","A8","A9",
			"B1","B2","B3","B4","B5","B6","B7","B8","B9",
			"C1","C2","C3","C4","C5","C6","C7","C8","C9",
			"D1","D2","D3","D4","D5","D6","D7","D8","D9",
			"E1","E2","E3","E4","E5","E6","E7","E8","E9",
			"F1","F2","F3","F4","F5","F6","F7","F8","F9",
			"G1","G2","G3","G4","G5","G6","G7","G8","G9",
			"H1","H2","H3","H4","H5","H6","H7","H8","H9"
			];

		
		
		    $("#addpaymentform5").validate({
			  rules: {
				unit: {
					required:true,
					minlength:2,
					maxlength:2
				},
				amount: {
					required:true,
					number:true,
					range: [1, 99999]
				},
				date_received:{
					required:true,
					date:true
				},
				receipt_no:{
					maxlength:10
				}
			  }
			});

			
			$("#addpaymentform").dialog('open');
						$("#unit").autocomplete({
				source: units
			});
			
		});
		$.fx.speeds._default = 1000;



	}
	

	
	
	
	
	//CHANGE RESIDENT PASSWORD
	function viewChangePasswordForm(){
		$("#changePasswordForm2").load("ajax/forms/changepassword.php", function(){
    $("#changepasswordform5").validate({
	  rules: {
		"new": 
			{
			required:true,
			minlength:6,
			maxlength:20
			}
	  }
	});

			$("input:submit", "#changePasswordForm2").button();
			$.fx.speeds._default = 1000;
			$("#changePasswordForm2").dialog({
			width:200,
			modal: true,
			resizable: false,
			show: "blind",
			hide: "explode",
			buttons: {
				Close: function() {
					$(this).dialog('close');
				}
			}
			});
		});
		
	}
	
	
	//CHANGE ADMIN PASSWORD
	
	function changeAdminPassword(){	
	$("#changePasswordForm3").load("ajax/forms/adminchangepassword.php", function(){
	$("#adminchangepasswordform5").validate({
			  rules: {
				old: {
					required:true,
					minlength:5,
					maxlength:20
				},
				"new": {
					required:true,
					maxlength:20,
					minlength:5
				},
				repeat: {
					equalTo: "#new"
				}
			}
	});
	$("input:submit", "#changePasswordForm3").button();
		$("#changePasswordForm3").dialog({	
			modal: true,
			resizable: false,
			width:250,
			show: "blind",
			hide: "explode",
			buttons: {
				Close : function() {
					$(this).dialog('close');
				}
			}
		});
	});
	}

	
	
	function changePassword(){
		newpass= $("#new").val();
		$.post('controllers/ChangePasswordController.php?mode=2',{ 
		newpass: newpass,
		unit_name: unit
		}, function(data) {
			details=data;
			displayChangePasswordDetails();
		});
		return false;
	}

	function displayChangePasswordDetails(){
		$("#changePasswordForm2").html(details);
	}
	
		function requestChangePassword(){
		old=$("#old").val();
		new1=$("#new").val();
		repeat=$("#repeat").val();
		$("#changePasswordForm3").html("<img src='gifs/indicator.gif' />");
		$.post('controllers/ChangePasswordController.php',{ 
		oldpass: old, 
		newpass: new1,
		repeat: repeat,
		}, function(data) {
			details=data;
			displayAdminChangePasswordDetails();
		});
		
		function displayAdminChangePasswordDetails(){
			$("#changePasswordForm3").html(details);
		}
		
		

		
	}


	

	<?php }?>
